<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class Lesson extends Model{
    use SoftDeletes;//使用软删除的trait
    //模型关联的数据表名（不含前缀）
    protected $table      = "lesson";//
    //关联数据表的主键（默认的之间为ID）
    protected $primaryKey = "lesson_id";
    //关联数据表可操作的字段
    protected $fillable    = ['course_id','lesson_name','cover_img','video_address','lesson_desc','lesson_duration','teacher_ids','lesson_desc'];
    //设置软删除
    protected $dates = ['deleted_at'];

    public function course(){
        return $this->belongsTo('App\Http\Models\Course','course_id','course_id');
    }
    public function teacher(){
        //第一个参数 需要关联的模型类名
        //第二个参数 关联的表名
        //第三个参数 当前表跟中间表对应的外键
        //第四个参数 要关联的表跟中间表对应的外键
        return $this->belongsToMany('App\Http\Models\Teacher','lesson_teacher','lesson_id','teacher_id');
    }
    //获取老师姓名
    public function getTeacherName($ids){
        $ids = json_decode($ids);
        $teacher_name = Teacher::whereIn('teacher_id',$ids)->pluck('teacher_name');
        $teacher_name = $teacher_name->toArray();
        $teacher_name = implode('|',$teacher_name);
        return $teacher_name;
    }
}

